package remato.client.view.editor.templateselection;

import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import remato.client.controller.RequirementPlugin;
import remato.client.model.CategoryWrapper;
import remato.client.model.PropertySetWrapper;
import remato.client.model.facade.IModelPersistable;
import remato.client.view.editor.AbstractFormPage;
import remato.client.view.editor.IModelFormPart;
import remato.client.view.util.ExtendedViewerSorter;
import remato.client.view.util.ModelItemLabelProvider;
import remato.client.view.util.contentproviders.ModelTreeViewContentProvider;
import remato.client.view.util.viewfilters.PropertyDefinitionSetViewFilter;

/**
 * @author petterei
 *
 * @version $Id: TemplateSelectionSectionPart.java,v 1.4 2006-02-01 12:49:23 petterei Exp $
 */
public class TemplateSelectionSectionPart extends SectionPart implements IModelFormPart {
	
	private CheckboxTableViewer viewer = null;
	private CheckStateListenerImpl checkListener = null;
	
	public TemplateSelectionSectionPart(FormPage page, Composite parent) {
		super(page.getManagedForm().getToolkit().createSection(parent, Section.DESCRIPTION | ExpandableComposite.TITLE_BAR | ExpandableComposite.TWISTIE | ExpandableComposite.EXPANDED));
	}

	/**
	 * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
	 */
	@Override
	public void initialize(IManagedForm form) {
		super.initialize(form);
		//		createContent(getManagedForm());
		createContent(getSection(), getManagedForm().getToolkit());
	}

    private void createContent(Section section, FormToolkit toolkit) {
		section.setText("Property templates");
		section.setDescription("Mark the templetes to add them for requirements of this category."); 
		section.marginHeight = 3;
		section.setLayout(AbstractFormPage.createLayout());
		Composite detailsclient = toolkit.createComposite(section);											
		FillLayout layout = (FillLayout)AbstractFormPage.createLayout();
		layout.marginWidth = 1;
		detailsclient.setLayout(layout);
	    section.setClient(detailsclient);
    	viewer = CheckboxTableViewer.newCheckList(detailsclient, SWT.MULTI);
	    viewer.setSorter(new ExtendedViewerSorter());
		viewer.setLabelProvider(new ModelItemLabelProvider());
		viewer.setContentProvider(new ModelTreeViewContentProvider());
		viewer.addFilter(new PropertyDefinitionSetViewFilter());
	    toolkit.paintBordersFor(detailsclient);
	    toolkit.adapt(viewer.getControl(), true, true);
	}

	@Override
	public boolean setFormInput(Object input) {
		if (null != checkListener) {
			viewer.removeCheckStateListener(checkListener);
		}
		if (null != input && input instanceof CategoryWrapper) {
			CategoryWrapper wrapper = (CategoryWrapper)input;
			viewer.setInput(RequirementPlugin.getInstance().getSession().getWrapperFactory().getSystem(PropertySetWrapper.class));
			Object[] setWrappers = wrapper.getPropertyDefinitionSets().toArray();
			viewer.setCheckedElements(setWrappers);
			checkListener = new CheckStateListenerImpl(wrapper);
			viewer.addCheckStateListener(checkListener);
//			getManagedForm().reflow(true); //.getForm().getBody().layout();
			return true;
		}
		viewer.setInput(null);
		return false;
	}

	@Override
	public void refresh() {
		viewer.refresh();
		super.refresh();
	}

	public void onModelUpdate(@SuppressWarnings("unused") IModelPersistable item) {
		refresh();
	}

}
class CheckStateListenerImpl implements ICheckStateListener {
	private CategoryWrapper wrapper = null ;
	
	public CheckStateListenerImpl(CategoryWrapper wrapper) {
		this.wrapper = wrapper;
	}

	public void checkStateChanged(CheckStateChangedEvent event) {
		Object object = event.getElement();
		if (object != null && object instanceof PropertySetWrapper){
			PropertySetWrapper definition = (PropertySetWrapper)object;
			if (event.getChecked()) {
				wrapper.addPropertyDefinitionSetWrapper(definition);
			} else {
				wrapper.removePropertyDefinitionSetWrapper(definition);
			}
		}
	}
	
}